package boot.spring.service.impl;

import boot.spring.mapper.LabApplyMapper;
import boot.spring.po.Lab;
import boot.spring.service.LabService;
import org.activiti.engine.IdentityService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.DEFAULT,timeout=5)
@Service
public class LabServiceImpl implements LabService {
	@Autowired
	LabApplyMapper labApplyMapper;
	@Autowired
	IdentityService identityservice;
	@Autowired
	RuntimeService runtimeservice;
	@Autowired
	TaskService taskservice;

	@Override
	public ProcessInstance startWorkflow(Lab apply, String userid, Map<String, Object> variables) {
		apply.setUser_id(userid);
		labApplyMapper.save(apply);
		String businesskey=String.valueOf(apply.getId());//使用leaveapply表的主键作为businesskey,连接业务数据和流程数据
		identityservice.setAuthenticatedUserId(userid);
		ProcessInstance instance=runtimeservice.startProcessInstanceByKey("lab_apply",businesskey,variables);
		System.out.println(businesskey);
		String instanceid=instance.getId();
		apply.setProcess_instance_id(instanceid);
		System.out.println(apply.toString());
		labApplyMapper.updateByPrimaryKey(apply);
		return instance;
	}




	@Override
	public Lab getlab(int id) {
		Lab lab=labApplyMapper.getlabApply(id);
		return lab;
	}


    @Override
    public List<Lab> getalldepttask(String userid) {
        List<Task> tasks=taskservice.createTaskQuery().taskCandidateGroup("管理员").list();
        List<Lab> results=new ArrayList<Lab>();
//        加用户权限   你是管理员才去查询，不然返回空
        for(Task task:tasks){
            String instanceid=task.getProcessInstanceId();
            ProcessInstance ins=runtimeservice.createProcessInstanceQuery().processInstanceId(instanceid).singleResult();
            String businesskey=ins.getBusinessKey();
            Lab a=labApplyMapper.getlabApply(Integer.parseInt(businesskey));
            a.setTask_id(task.getId());
//            a.setTask(task);
            results.add(a);
        }

        return results;
    }

	@Override
	public List<Lab> getupdateapplytask(String userid) {
		List<Lab> results=new ArrayList<Lab>();
		List<Task> tasks=taskservice.createTaskQuery().taskCandidateOrAssigned(userid).taskName("调整申请").list();
		for(Task task:tasks){
			String instanceid=task.getProcessInstanceId();
			ProcessInstance ins=runtimeservice.createProcessInstanceQuery().processInstanceId(instanceid).singleResult();
			String businesskey=ins.getBusinessKey();
			Lab a=labApplyMapper.getlabApply(Integer.parseInt(businesskey));
			a.setTask_id(task.getId());
//			a.setTask(task);
			results.add(a);
		}
		return results;
	}

	@Override
	public int getallupdateapplytask(String userid) {
		List<Task> tasks=taskservice.createTaskQuery().taskCandidateOrAssigned(userid).taskName("调整申请").list();
		return tasks.size();
	}



	@Override
	public void updatecomplete(String taskid, Lab lab, String reapply) {
		Task task=taskservice.createTaskQuery().taskId(taskid).singleResult();
		String instanceid=task.getProcessInstanceId();
		ProcessInstance ins=runtimeservice.createProcessInstanceQuery().processInstanceId(instanceid).singleResult();
		String businesskey=ins.getBusinessKey();
		Lab a=labApplyMapper.getlabApply(Integer.parseInt(businesskey));
		a.setCreate_time(lab.getCreate_time());
		a.setUpdate_time(lab.getUpdate_time());
		a.setInstruction_class_id(lab.getInstruction_class_id());
		a.setNumber(lab.getNumber());
		Map<String,Object> variables=new HashMap<String,Object>();
		variables.put("reapply", reapply);
		if(reapply.equals("true")){
			labApplyMapper.updateByPrimaryKey(a);
			taskservice.complete(taskid,variables);
		}else
			taskservice.complete(taskid,variables);
	}


}

